# 定义简单的栈类
class Stack:
    # 构造函数
    def __init__(self):
        self.items = []

    # 压栈
    def push(self, item):
        self.items.append(item)

    # 出栈
    def pop(self):
        return self.items.pop()

    # 获取栈顶元素
    def peek(self):
        return self.items[len(self.items) - 1]

    # 检查栈是否为空
    def empty(self):
        return len(self.items) == 0

def box(n, nums):
    # 用于储存结果
    ret = Stack()
    # 遍历n次，如果发现num等于零，先看栈是否为空，为空则不处理，不为空则弹出
    # 如果num不等于零，则直接压栈
    for num in nums:
        if num == 0 and not ret.empty():
            ret.pop()
            continue
        ret.push(num)
    return ret


ret1 = box(5, [1,2,0,3,8])
# 遍历栈，打印结果
while not ret1.empty():
    print(ret1.pop())
print()

ret2 = box(7, [6,2,0,5,8,0,7])
# 遍历栈，打印结果
while not ret2.empty():
    print(ret2.pop())
print()

